package mvc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.jasper.tagplugins.jstl.core.If;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import bean.entity.Book;
import bean.entity.User;
import dao.BookDao;
import dao.UserDao;
import dao.test.BookDaoByBatis;
import jakarta.servlet.http.HttpSession;
import service.BookService;
import service.PurchaseService;
import service.UserService;
import spring.SpringContextUtil;

@Controller
@RequestMapping("/store")
public class StoreController {
	{
		System.out.println("Store Controller started");
	}
	@GetMapping
	public String loadPage(Model model , HttpSession session) {
		var name = (String)session.getAttribute("name");
		UserService us = (UserService) SpringContextUtil.getBean("userService");

		
		if (name != null) {
//			System.out.println("User");
			model.addAttribute("visitorType", name);
			model.addAttribute("actionName" , "退出");
			model.addAttribute("actionUrl","'./logout'");
			model.addAttribute("AccountStatus", "余额为"+String.valueOf(us.getUser(name).getBalance()));
		}else {
//			System.out.println("Vistor");
			model.addAttribute("visitorType", "游客");
			model.addAttribute("actionName","登录");
			model.addAttribute("actionUrl","'./login'");
			model.addAttribute("AccountStatus", "");
		}
		return "store";
	}
	
	@ResponseBody
	@PostMapping("/getBooks")
	public List<Book> getBooks(){
		System.out.println("Page request for Books");
		BookService bs = (BookService) SpringContextUtil.getBean("bookService");
		Book book = new Book("0","老人与海","歪比巴卜",10,5);
		bs.add(book);
//		System.out.println(book.getName());
		
		return bs.getBooks();
	}
	
	@ResponseBody
	@PostMapping("/purchase")
	public Map<String,String> purchase (@RequestBody Map<String, Book>data , HttpSession session){
		System.out.println("User try to purchase");
		
		PurchaseService ps = (PurchaseService) SpringContextUtil.getBean("purchaseService");
//		UserService us = (UserService)SpringContextUtil.getBean("userService");
		UserDao ud = (UserDao)SpringContextUtil.getBean("userDao");
		BookDaoByBatis bd = (BookDaoByBatis)SpringContextUtil.getBean("bookDao");
		
		Book book = (Book)data.get("book");
		String name = (String)	session.getAttribute("name");
		
//		System.out.println(name);
		User u = ud.findByName(name);
//		System.out.println(u.getName());
		
		Map<String, String> res = new HashMap<String, String>();
		res.put("status", 
				ps.buy(book, u, 1).toString()
				);
		System.out.println( "The rest books : \t" + bd.DB.get(0).getAmount());
		return res;
	}
	
	@ResponseBody
	@PostMapping("/getAccount")
	public Map<String,Integer> getAccount (HttpSession session) {
		UserService us = (UserService) SpringContextUtil.getBean("userService");
		String name = (String) session.getAttribute("name");
		
		Map<String, Integer> res = new HashMap<String, Integer>();
		if (name != null) {
			res.put("balance", us.getUser(name).getBalance());
		}else {
			res.put("balance", -1);
		}
		
		return res;
	}
}
